<?php

namespace App\Http\Controllers;


use App\Models\Message;
use App\Models\PartCompany;
use App\Models\Project;
use App\Models\User;
use App\Services\CommonService;
use App\Services\MessageService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

class IndexController extends Controller
{
    public $messageService;

    public function __construct(MessageService $messageService)
    {
        parent::__construct();
        $this->messageService = $messageService;
    }

    function index()
    {
        return view('welcome');
    }

    /**
     * 页面当前位置
     */
    /**
     * @param Request $request
     * @return JsonResponse
     * @author: lxk

     */
    public function nowLocation(Request $request) : JsonResponse
    {
        $params = $request->input();
        $nowLocation = '页面导航：项目列表';
        if ($params['project_id']) {
            $projectName = Project::query()->where('id', $params['project_id'])->value('title');
            $nowLocation = '页面导航：'. $projectName.' / 参见单位列表';
            if ($params['part_company_id']) {
                $partCompanyName = PartCompany::query()->where('id', $params['part_company_id'])->value('title');
                $nowLocation = '页面导航：'. $projectName.' / '.$partCompanyName .' / 案卷目录列表';
                if ($params['book_title_id']) {
                    $bookTitleInstance = CommonService::getBookTitleInstance($params['part_company_id'], $params['project_classify_id']);
                    $archivesNo = $bookTitleInstance->where('id', $params['book_title_id'])->value('archives_no');
                    $nowLocation = '页面导航：'. $projectName.' / '.$partCompanyName.' / 档号：'.$archivesNo.' / 卷内目录列表';
                }
            }
        }
        $data = [
            'location' => $nowLocation
        ];
        return $this->success($data);
    }

    /**
     * 各种模板下载
     */
    public function exampleTemplate(Request $request)
    {
        if (!$type = $request->input('type')) {
            return $this->error('请指定类型', 400);
        }
        switch ($type) {
            case 1:
                $file = '用户导入模板.xlsx';
                break;
            case 2:
                $file = '授权书模板.jpg';
                break;
            case 3:
                $file = '29号文案卷模板.xlsx';
                break;
            case 4:
                $file = '29号文卷内模板.xlsx';
                break;
            case 5:
                $file = '8号文案卷模板.xlsx';
                break;
            case 6:
                $file = '8号文卷内模板.xlsx';
                break;
            case 7:
                $file = '照片案卷模板.xlsx';
                break;
            case 8:
                $file = '照片卷内模板.xlsx';
                break;
            case 9:
                $file = '单位工程模板.xlsx';
                break;
            case 10:
                $file = '文书档案模板.xls';
                break;
            case 11:
                $file = '兰渝格式案卷模板.xlsx';
                break;
            case 12:
                $file = '兰渝格式卷内模板.xlsx';
                break;
            case 13:
                $file = '业务规范模板.xlsx';
                break;
            case 14:
                $file = '公司档案管理制度模板.xlsx';
                break;
            case 15:
                $file = '档案业务指导工作记录模板.xlsx';
                break;
            case 16:
                $file = '专项检査意见及整改报告模板.xlsx';
                break;
            case 17:
                $file = '招投标台账模板.xlsx';
                break;
            case 18:
                $file = '合同台账模板.xlsx';
                break;
            case 19:
                $file = '物质设备台账模板.xlsx';
                break;
            case 20:
                $file = '设计变更台账模板.xlsx';
                 break;
            case 21:
                $file = '库房档案架模板.xlsx';
                break;
            case 22:
                $file = '档案室温湿度登记表模板.xlsx';
                break;
            case 23:
                $file = '会计档案模板.xlsx';
                break;
            default:
                $file = '';
        }
        return Storage::disk('public')->download('/downTemplates/'.$file);
    }

    /**
     * 分页数组
     */
    public function perPageArr()
    {
        $data = [10, 20, 30, 40, 50];

        return $this->success($data);
    }

    /**
     * 当前用户看到的消息
     */
    public function getMessages()
    {
        $list = Message::query()
            ->companyId()
            ->userId()
            ->createdAt()
            ->select('id', 'type', 'content', 'object_id', 'is_read', 'created_at')
            ->orderBy('is_read')
            ->orderByDesc('created_at')
            ->get();
        if ($list->isEmpty()) {
            return [
                'list' => [],
                'count'=> 0
            ];
        }
        $list = $list->toArray();
        foreach ($list as &$val) {
            $val['created_at'] = Carbon::parse($val['created_at'])->format('Y-m-d H:i');
            $val['for_huaman'] = Carbon::parse($val['created_at'])->diffForHumans();
        }
        return [
            'list'  => $list,
            'count' => count($list)
        ];
    }


}
